package com.fmsz.xmly.fragments.findfgts;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.AnchorAdapter;
import com.fmsz.xmly.entitys.findanchor.AnchorEntity;
import com.fmsz.xmly.utils.Urls;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class FindAnchorFragment extends Fragment {

    private RecyclerView recyclerview;
    private AnchorAdapter adapter;
    private ArrayList datas = new ArrayList();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_find__anchor_, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view){
        recyclerview = (RecyclerView) view.findViewById(R.id.anchor_recycler);
        adapter = new AnchorAdapter(getActivity(),datas);
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview.setAdapter(adapter);
    }

    private void initData() {
        RequestQueue requestQuene = Volley.newRequestQueue(getActivity());
        StringRequest stringrequest = new StringRequest(Request.Method.GET, Urls.FIND_ANCHOR, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray array = new JSONObject(response).getJSONArray("list");
                    List list = JSON.parseArray(array.toString(), AnchorEntity.class);
                    datas.addAll(list);
                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },null);
        requestQuene.add(stringrequest);
    }

}
